home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / sun4.md / gdb / RCS / infptrace.c,v < prev    next >
Encoding:
Text File  |  1992-06-24  |  10.9 KB  |  455 lines

  1. head     1.1;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.1
  10. date     92.06.09.14.26.45;  author secor;  state Exp;
  11. branches ;
  12. next     ;
  13.  
  14.  
  15. desc
  16. @@
  17.  
  18.  
  19.  
  20. 1.1
  21. log
  22. @Initial revision
  23. @
  24. text
  25. @/* Low level Unix child interface to ptrace, for GDB when running under Unix.
  26.    Copyright 1988, 1989, 1990, 1991, 1992 Free Software Foundation, Inc.
  27.  
  28. This file is part of GDB.
  29.  
  30. This program is free software; you can redistribute it and/or modify
  31. it under the terms of the GNU General Public License as published by
  32. the Free Software Foundation; either version 2 of the License, or
  33. (at your option) any later version.
  34.  
  35. This program is distributed in the hope that it will be useful,
  36. but WITHOUT ANY WARRANTY; without even the implied warranty of
  37. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  38. GNU General Public License for more details.
  39.  
  40. You should have received a copy of the GNU General Public License
  41. along with this program; if not, write to the Free Software
  42. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  43.  
  44. #include "defs.h"
  45. #include "frame.h"
  46. #include "inferior.h"
  47. #include "target.h"
  48.  
  49. #ifdef USG
  50. #include <sys/types.h>
  51. #endif
  52.  
  53. #include <syscall.h>
  54. #include <sys/param.h>
  55. #include <sys/dir.h>
  56. #include <signal.h>
  57. #include <sys/ioctl.h>
  58. #ifndef USG
  59. #include <sys/ptrace.h>
  60. #endif
  61.  
  62. #if !defined (PT_KILL)
  63. #define PT_KILL 8
  64. #define PT_STEP 9
  65. #define PT_CONTINUE 7
  66. #define PT_READ_U 3
  67. #define PT_WRITE_U 6
  68. #define PT_READ_I 1
  69. #define    PT_READ_D 2
  70. #define PT_WRITE_I 4
  71. #define PT_WRITE_D 5
  72. #endif /* No PT_KILL.  */
  73.  
  74. #ifndef PT_ATTACH
  75. #define PT_ATTACH PTRACE_ATTACH
  76. #endif
  77. #ifndef PT_DETACH
  78. #define PT_DETACH PTRACE_DETACH
  79. #endif
  80.  
  81. #include "gdbcore.h"
  82. #ifndef    NO_SYS_FILE
  83. #include <sys/file.h>
  84. #endif
  85. #include <sys/stat.h>
  86.  
  87. #if !defined (FETCH_INFERIOR_REGISTERS)
  88. #include <sys/user.h>        /* Probably need to poke the user structure */
  89. #if defined (KERNEL_U_ADDR_BSD)
  90. #include <a.out.h>        /* For struct nlist */
  91. #endif /* KERNEL_U_ADDR_BSD.  */
  92. #endif /* !FETCH_INFERIOR_REGISTERS */
  93.  
  94.  
  95. /* This function simply calls ptrace with the given arguments.  
  96.    It exists so that all calls to ptrace are isolated in this 
  97.    machine-dependent file. */
  98. int
  99. call_ptrace (request, pid, addr, data)
  100.      int request, pid, *addr, data;
  101. {
  102.   return ptrace (request, pid, addr, data);
  103. }
  104.  
  105. #ifdef DEBUG_PTRACE
  106. /* For the rest of the file, use an extra level of indirection */
  107. /* This lets us breakpoint usefully on call_ptrace. */
  108. #define ptrace call_ptrace
  109. #endif
  110.  
  111. /* This is used when GDB is exiting.  It gives less chance of error.*/
  112.  
  113. void
  114. kill_inferior_fast ()
  115. {
  116.   if (inferior_pid == 0)
  117.     return;
  118.   ptrace (PT_KILL, inferior_pid, 0, 0);
  119.   wait ((int *)0);
  120. }
  121.  
  122. void
  123. kill_inferior ()
  124. {
  125.   kill_inferior_fast ();
  126.   target_mourn_inferior ();
  127. }
  128.  
  129. /* Resume execution of the inferior process.
  130.    If STEP is nonzero, single-step it.
  131.    If SIGNAL is nonzero, give it that signal.  */
  132.  
  133. void
  134. child_resume (step, signal)
  135.      int step;
  136.      int signal;
  137. {
  138.   errno = 0;
  139.  
  140.   /* An address of (int *)1 tells ptrace to continue from where it was. 
  141.      (If GDB wanted it to start some other way, we have already written
  142.      a new PC value to the child.)  */
  143.  
  144.   if (step)
  145. #if 0
  146. /*def NO_SINGLE_STEP*/
  147.     single_step (signal);
  148. #else    
  149.     ptrace (PT_STEP, inferior_pid, (int *)1, signal);
  150. #endif
  151.   else
  152. #ifdef AIX_BUGGY_PTRACE_CONTINUE
  153.     AIX_BUGGY_PTRACE_CONTINUE;
  154. #else
  155.     ptrace (PT_CONTINUE, inferior_pid, (int *)1, signal);
  156. #endif
  157.  
  158.   if (errno)
  159.     perror_with_name ("ptrace");
  160. }
  161.  
  162. #ifdef ATTACH_DETACH
  163. /* Nonzero if we are debugging an attached process rather than
  164.    an inferior.  */
  165. extern int attach_flag;
  166.  
  167. /* Start debugging the process whose number is PID.  */
  168. int
  169. attach (pid)
  170.      int pid;
  171. {
  172.   errno = 0;
  173.   ptrace (PT_ATTACH, pid, 0, 0);
  174.   if (errno)
  175.     perror_with_name ("ptrace");
  176.   attach_flag = 1;
  177.   return pid;
  178. }
  179.  
  180. /* Stop debugging the process whose number is PID
  181.    and continue it with signal number SIGNAL.
  182.    SIGNAL = 0 means just continue it.  */
  183.  
  184. void
  185. detach (signal)
  186.      int signal;
  187. {
  188.   errno = 0;
  189.   ptrace (PT_DETACH, inferior_pid, 1, signal);
  190.   if (errno)
  191.     perror_with_name ("ptrace");
  192.   attach_flag = 0;
  193. }
  194. #endif /* ATTACH_DETACH */
  195.  
  196. #if !defined (FETCH_INFERIOR_REGISTERS)
  197.  
  198. /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
  199.    to get the offset in the core file of the register values.  */
  200. #if defined (KERNEL_U_ADDR_BSD)
  201. /* Get kernel_u_addr using BSD-style nlist().  */
  202. CORE_ADDR kernel_u_addr;
  203.  
  204. void
  205. _initialize_kernel_u_addr ()
  206. {
  207.   struct nlist names[2];
  208.  
  209.   names[0].n_un.n_name = "_u";
  210.   names[1].n_un.n_name = NULL;
  211.   if (nlist ("/vmunix", names) == 0)
  212.     kernel_u_addr = names[0].n_value;
  213.   else
  214.     fatal ("Unable to get kernel u area address.");
  215. }
  216. #endif /* KERNEL_U_ADDR_BSD.  */
  217.  
  218. #if defined (KERNEL_U_ADDR_HPUX)
  219. /* Get kernel_u_addr using HPUX-style nlist().  */
  220. CORE_ADDR kernel_u_addr;
  221.  
  222. struct hpnlist {      
  223.         char *          n_name;
  224.         long            n_value;  
  225.         unsigned char   n_type;   
  226.         unsigned char   n_length;  
  227.         short           n_almod;   
  228.         short           n_unused;
  229. };
  230. static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
  231.  
  232. /* read the value of the u area from the hp-ux kernel */
  233. void _initialize_kernel_u_addr ()
  234. {
  235.     nlist ("/hp-ux", &nl);
  236.     kernel_u_addr = nl[0].n_value;
  237. }
  238. #endif /* KERNEL_U_ADDR_HPUX.  */
  239.  
  240. #if !defined (offsetof)
  241. #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
  242. #endif
  243.  
  244. /* U_REGS_OFFSET is the offset of the registers within the u area.  */
  245. #if !defined (U_REGS_OFFSET)
  246. #define U_REGS_OFFSET \
  247.   ptrace (PT_READ_U, inferior_pid, \
  248.       (int *)(offsetof (struct user, u_ar0)), 0) - KERNEL_U_ADDR
  249. #endif
  250.  
  251. /* Registers we shouldn't try to fetch.  */
  252. #if !defined (CANNOT_FETCH_REGISTER)
  253. #define CANNOT_FETCH_REGISTER(regno) 0
  254. #endif
  255.  
  256. /* Fetch one register.  */
  257.  
  258. static void
  259. fetch_register (regno)
  260.      int regno;
  261. {
  262.   register unsigned int regaddr;
  263.   char buf[MAX_REGISTER_RAW_SIZE];
  264.   char mess[128];                /* For messages */
  265.   register int i;
  266.  
  267.   /* Offset of registers within the u area.  */
  268.   unsigned int offset;
  269.  
  270.   if (CANNOT_FETCH_REGISTER (regno))
  271.     {
  272.       bzero (buf, REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
  273.       supply_register (regno, buf);
  274.       return;
  275.     }
  276.  
  277.   offset = U_REGS_OFFSET;
  278.  
  279.   regaddr = register_addr (regno, offset);
  280.   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  281.     {
  282.       errno = 0;
  283.       *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, (int *)regaddr, 0);
  284.       regaddr += sizeof (int);
  285.       if (errno != 0)
  286.     {
  287.       sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
  288.       perror_with_name (mess);
  289.     }
  290.     }
  291.   supply_register (regno, buf);
  292. }
  293.  
  294.  
  295. /* Fetch all registers, or just one, from the child process.  */
  296.  
  297. void
  298. fetch_inferior_registers (regno)
  299.      int regno;
  300. {
  301.   if (regno == -1)
  302.     for (regno = 0; regno < NUM_REGS; regno++)
  303.       fetch_register (regno);
  304.   else
  305.     fetch_register (regno);
  306. }
  307.  
  308. /* Registers we shouldn't try to store.  */
  309. #if !defined (CANNOT_STORE_REGISTER)
  310. #define CANNOT_STORE_REGISTER(regno) 0
  311. #endif
  312.  
  313. /* Store our register values back into the inferior.
  314.    If REGNO is -1, do this for all registers.
  315.    Otherwise, REGNO specifies which register (so we can save time).  */
  316.  
  317. void
  318. store_inferior_registers (regno)
  319.      int regno;
  320. {
  321.   register unsigned int regaddr;
  322.   char buf[80];
  323.   extern char registers[];
  324.   register int i;
  325.  
  326.   unsigned int offset = U_REGS_OFFSET;
  327.  
  328.   if (regno >= 0)
  329.     {
  330.       regaddr = register_addr (regno, offset);
  331.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  332.     {
  333.       errno = 0;
  334.       ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
  335.           *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  336.       if (errno != 0)
  337.         {
  338.           sprintf (buf, "writing register number %d(%d)", regno, i);
  339.           perror_with_name (buf);
  340.         }
  341.       regaddr += sizeof(int);
  342.     }
  343.     }
  344.   else
  345.     {
  346.       for (regno = 0; regno < NUM_REGS; regno++)
  347.     {
  348.       if (CANNOT_STORE_REGISTER (regno))
  349.         continue;
  350.       regaddr = register_addr (regno, offset);
  351.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  352.         {
  353.           errno = 0;
  354.           ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
  355.               *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  356.           if (errno != 0)
  357.         {
  358.           sprintf (buf, "writing register number %d(%d)", regno, i);
  359.           perror_with_name (buf);
  360.         }
  361.           regaddr += sizeof(int);
  362.         }
  363.     }
  364.     }
  365. }
  366. #endif /* !defined (FETCH_INFERIOR_REGISTERS).  */
  367.  
  368. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  369.    in the NEW_SUN_PTRACE case.
  370.    It ought to be straightforward.  But it appears that writing did
  371.    not write the data that I specified.  I cannot understand where
  372.    it got the data that it actually did write.  */
  373.  
  374. /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
  375.    to debugger memory starting at MYADDR.   Copy to inferior if
  376.    WRITE is nonzero.
  377.   
  378.    Returns the length copied, which is either the LEN argument or zero.
  379.    This xfer function does not do partial moves, since child_ops
  380.    doesn't allow memory operations to cross below us in the target stack
  381.    anyway.  */
  382.  
  383. int
  384. child_xfer_memory (memaddr, myaddr, len, write, target)
  385.      CORE_ADDR memaddr;
  386.      char *myaddr;
  387.      int len;
  388.      int write;
  389.      struct target_ops *target;        /* ignored */
  390. {
  391.   register int i;
  392.   /* Round starting address down to longword boundary.  */
  393.   register CORE_ADDR addr = memaddr & - sizeof (int);
  394.   /* Round ending address up; get number of longwords that makes.  */
  395.   register int count
  396.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  397.   /* Allocate buffer of that many longwords.  */
  398.   register int *buffer = (int *) alloca (count * sizeof (int));
  399.  
  400.   if (write)
  401.     {
  402.       /* Fill start and end extra bytes of buffer with existing memory data.  */
  403.  
  404.       if (addr != memaddr || len < (int)sizeof (int)) {
  405.     /* Need part of initial word -- fetch it.  */
  406.         buffer[0] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
  407.       }
  408.  
  409.       if (count > 1)        /* FIXME, avoid if even boundary */
  410.     {
  411.       buffer[count - 1]
  412.         = ptrace (PT_READ_I, inferior_pid,
  413.               (int *)(addr + (count - 1) * sizeof (int)), 0);
  414.     }
  415.  
  416.       /* Copy data to be written over corresponding part of buffer */
  417.  
  418.       bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  419.  
  420.       /* Write the entire buffer.  */
  421.  
  422.       for (i = 0; i < count; i++, addr += sizeof (int))
  423.     {
  424.       errno = 0;
  425.       ptrace (PT_WRITE_D, inferior_pid, (int *)addr, buffer[i]);
  426.       if (errno)
  427.         {
  428.           /* Using the appropriate one (I or D) is necessary for
  429.          Gould NP1, at least.  */
  430.           errno = 0;
  431.           ptrace (PT_WRITE_I, inferior_pid, (int *)addr, buffer[i]);
  432.         }
  433.       if (errno)
  434.         return 0;
  435.     }
  436.     }
  437.   else
  438.     {
  439.       /* Read all the longwords */
  440.       for (i = 0; i < count; i++, addr += sizeof (int))
  441.     {
  442.       errno = 0;
  443.       buffer[i] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
  444.       if (errno)
  445.         return 0;
  446.       QUIT;
  447.     }
  448.  
  449.       /* Copy appropriate bytes out of the buffer.  */
  450.       bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  451.     }
  452.   return len;
  453. }
  454. @
  455.